
/*   RolGPS Project
 * 
 *   Copyright (C) 2009 Pablo Alejandro Costesich <rhfixer@gmail.com>
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package org.rolgps.libedb.engine.sqlite3.zentus;

import java.util.ArrayList;
import java.util.List;
import org.rolgps.libedb.exception.UnknownTypeException;

/**
 *
 * @author xeph
 */
public class Field implements org.rolgps.libedb.standard.Field {

    public static final String AUTOINCREMENT = "AUTOINCREMENT";
    public static final String INTEGER = "INTEGER";
    public static final String PRIMARYKEY = "PRIMARY KEY";
    public static final String IDFIELD = INTEGER + " " + PRIMARYKEY + " " + AUTOINCREMENT;
    public static final String STRING = "VARCHAR";
    private String tableName = null;
    private List<String> types = new ArrayList();
    private boolean hasDataType = false;
    private boolean isPrimaryOrforeignKey = false;

    public Field(String nombre, String... listTypes) {
        this.tableName = nombre.trim();
        this.addType(listTypes);
    }

    public Field(String nombre) {
        this.tableName = nombre.trim();
    }

    public String getName() {
        return tableName;
    }

    public String[] getTypes() {
        //ordenar parametros
        return (String[]) types.toArray();
    }

    public void addType(String... types) {
        for (String type : types) {

            //verificar que no exista un tipo duplicado

            //si es un entero o un string y NO tiene un tipo de dato, anyadir
            if ((type.equals(INTEGER) || type.equals(STRING)) && !this.hasDataType) {
                this.types.add(type);
                this.hasDataType = true;
            } else if ((type.equals(PRIMARYKEY)) && !this.isPrimaryOrforeignKey) {
                this.types.add(type);
                this.isPrimaryOrforeignKey = true;
            } else if (type.equals(AUTOINCREMENT)) {
                boolean hasInteger = false;
                boolean hasPrimaryKey = false;
                for (String eachType : this.types) {
                    if (eachType.equals(PRIMARYKEY)) {
                        hasPrimaryKey = true;
                    } else if (eachType.equals(INTEGER)) {
                        hasInteger = true;
                    }
                }
                if (hasInteger && hasPrimaryKey) {
                    this.types.add(type);
                }
            }
            if (type.equals(Field.IDFIELD)) {
                this.types.clear();
                this.types.add(type);
            }
        }
    }

    public void popType(int id) throws UnknownTypeException {
        if(id >= types.size() || id < 0){
            throw new UnknownTypeException("index out of bounds.\n" +
                    "Size: "+types.size()+", provided idx: "+id);
        }else{
            types.remove(id);
        }
    }

    public int getTypeId(String type) {
        return types.indexOf(type);
    }

    public void setName(String name) {
        this.tableName = name.trim();
    }
}
